package com.kkyy.msystem.page;

import lombok.Data;
import lombok.ToString;

@Data
@ToString
public class Page {
    // 页大小
    public static final Integer PAGE_SIZE = 5;
    // 起始页/当前页
    private Integer startPage;
    // 数据总量
    private Integer totalCount;
    // 总页数
    private int totalPage;
    // 末页
    private Integer lastPage;
    // 当前页
    private Integer currentPage;

    // 设置当前页
    public Integer getCurrentPage() {
        if (this.currentPage == null) {
            currentPage = 1;
        }
        return currentPage;
    }

    public Page(Integer startPage, Integer totalCount) {
        this.startPage = startPage;
        this.totalCount = totalCount;
    }

    // 当前页计算
    public Integer getStartPage() {
        if (startPage == null || startPage == 0) {
            this.currentPage = 1;
        } else {
            this.currentPage = startPage;
        }
        if (this.startPage == null) {
            this.startPage = 1;
        }
        if ((this.startPage - 1) * PAGE_SIZE < 0) {
            this.startPage = 0;
        } else {
            this.startPage = (this.startPage - 1) * PAGE_SIZE;
        }
        return startPage;
    }

    public Boolean isHasPrevious() {
        // 第一页是0，就不显示
        if (startPage == 0)
            return false;
        return true;
    }

    public Boolean isHasNext() {
        // 是最后一页
        if (startPage == getLastPage())
            return false;
        return true;
    }

    public Integer getLastPage() {
        getCurrentPage((int) Math.ceil(1.0 * totalCount / PAGE_SIZE));
        Integer pageSize = PAGE_SIZE;
        Integer lastPage;
        // 能除尽；50 % 5
        if (0 == totalCount % pageSize)
            // 最后一页起始为45
            lastPage = totalCount - pageSize;
        else
            // 除不尽，51 - 51 % 5
            lastPage = totalCount - totalCount % pageSize;
        lastPage = lastPage < 0 ? 0 : lastPage;
        return lastPage;
    }

    private void getCurrentPage(int ceil) {
        this.currentPage = ceil;
    }

    public void getTotalPage(Integer totalCount) {
        // 向上取整，50条数据10页，49条数据10页
        int totalPage = (int) Math.ceil(1.0 * totalCount / PAGE_SIZE);
        this.totalCount = totalCount;
        this.totalPage = totalPage;
    }

    public int getTotalPage() {
        return totalPage;
    }
}